//Place relevant data set in folder named "IPUMS International". 
// age inctot incearn incwage country bplcountry marst  resident sample edattain sex empstat school nativity edattaind
use "/IPUMS International/ipumsi_00001.dta", clear


keep if age>25 & age<65
keep if !missing(inctot) | !missing(incearn) | !missing(incwage)
drop if (inctot==9999998 | inctot==9999999) & ( incearn==9999998 |  incearn==9999999) & (incwage==9999998 | incwage==9999999)    //drop if no income variable
drop if country==840 //drop US as it completely dominates the sample otherwise (I have tried including, or using only, US data; results are robust.)
drop if resident==2 | resident==3 //drop if non-resident (only exists for SouthAfrica)


//Generate host country cnbr 
/*gen cnbr_h =0
replace  cnbr_h = X if country== XYZ matches country ID in other data.
...
*/
gen cnbr_h =0
replace cnbr_h=26 if country==76
replace cnbr_h=59 if country==124
replace cnbr_h=82 if country==214
replace cnbr_h=47 if country==356
replace cnbr_h=83 if country==388
replace cnbr_h=76 if country==484
replace cnbr_h=81 if country==591
replace cnbr_h=109 if country==710
replace cnbr_h=103 if country==724
replace cnbr_h=106 if country==728
replace cnbr_h=82 if country==780
replace cnbr_h=122 if country==858
replace cnbr_h=108 if country==862

//Generate birth country cnbr
//I only include country codes below that are included in the final regressions. As I am not allowed to show country-level results, I want to avoid "writing out" country names to the greatest extent possible; hence, many numbers in the 1-129 ecuence are missing below. When running the regressions, I included country-specific earnings profiles for all origins. 

gen cnbr_b=0
replace cnbr_b= 12  if bplcountry==23010
replace cnbr_b= 16  if bplcountry==31011
replace cnbr_b= 16  if bplcountry==33090

replace cnbr_b= 19  if bplcountry==51010
replace cnbr_b= 22  if bplcountry==44020
replace cnbr_b= 26  if bplcountry==23030
replace cnbr_b= 26  if bplcountry==23100

replace cnbr_b= 27  if bplcountry==41020
replace cnbr_b= 29  if bplcountry==23040
replace cnbr_b= 30  if bplcountry==23050
replace cnbr_b= 31  if bplcountry==42010
replace cnbr_b= 33  if bplcountry==13020
replace cnbr_b= 38  if bplcountry==34040
replace cnbr_b= 38  if bplcountry==43080

replace cnbr_b= 39  if bplcountry==33080
replace cnbr_b=40   if bplcountry==42040
replace cnbr_b=41   if bplcountry==44030
replace cnbr_b= 45  if bplcountry==15060

replace cnbr_b=46   if bplcountry==43060
replace cnbr_b= 47  if bplcountry==32040
replace cnbr_b= 48  if bplcountry==33040

replace cnbr_b= 50  if bplcountry==32050
replace cnbr_b=51   if bplcountry==42060
replace cnbr_b=52   if bplcountry==42050

replace cnbr_b=53   if bplcountry==34070
replace cnbr_b=54   if bplcountry==43070
replace cnbr_b= 55  if bplcountry==31020
replace cnbr_b= 56  if bplcountry==34080
replace cnbr_b= 57  if bplcountry==43140

replace cnbr_b= 59  if bplcountry==24020
replace cnbr_b=62   if bplcountry==31010

replace cnbr_b=64   if bplcountry==31030
replace cnbr_b=64   if bplcountry==31031
replace cnbr_b=64   if bplcountry==31032

replace cnbr_b=70  if bplcountry==34100
replace cnbr_b=73   if bplcountry==43130

replace cnbr_b=74   if bplcountry==33060
replace cnbr_b=75  if bplcountry==13040
replace cnbr_b=76  if bplcountry==22060
replace cnbr_b=79  if bplcountry==44080

replace cnbr_b= 80  if bplcountry==15130
replace cnbr_b=84   if bplcountry==42090
replace cnbr_b=85  if bplcountry==51020

replace cnbr_b=90  if bplcountry==23110
replace cnbr_b=91   if bplcountry==41050
replace cnbr_b=92   if bplcountry==43090
replace cnbr_b=93   if bplcountry==41060
replace cnbr_b=93   if bplcountry==41070

replace cnbr_b=94   if bplcountry==41080
replace cnbr_b=96   if bplcountry==44090
replace cnbr_b=97  if bplcountry==43142
replace cnbr_b=103   if bplcountry==43020
replace cnbr_b=103   if bplcountry==43120

replace cnbr_b=105   if bplcountry==42120
replace cnbr_b=106  if bplcountry==13050
replace cnbr_b=107   if bplcountry==42110

replace cnbr_b=109  if bplcountry==14040
replace cnbr_b=111  if bplcountry==31013
replace cnbr_b=113   if bplcountry==33100
replace cnbr_b=114   if bplcountry==41030
replace cnbr_b=117   if bplcountry==34160
replace cnbr_b=118   if bplcountry==44040
replace cnbr_b=121   if bplcountry==41040
replace cnbr_b=122   if bplcountry==23130
replace cnbr_b=123   if bplcountry==24040
replace cnbr_b=129   if bplcountry==44010



gen MarBin=0
replace MarBin= 1 if marst==2


gen age_sq=age^2
gen age_qu=age^3

gen Mig=0
replace Mig=1 if nativity==2

tab sample, gen(FE)

//Make income annual for countries that report monthlyn income (irrelevant except for comparing summary stats, given sample fixed effects).
replace inctot=inctot*12 if country==76 | country==214 | country==484 | country==591 | country==862
replace incearn=incearn*12 if country==76 | country==214 | country==484 | country==591 | country==862
replace incwage=incwage*12 if country==591 | country==780 | country==858
replace incwage=incwage*52 if country==356 


replace inctot = incwage if country==356 | country==388 | country==858

//Gen hourly wages
gen w_earn = incearn/hrswork1

gen log_inctot=ln(inctot)
gen logw_earn=ln(w_earn)

//Focus on male workers
drop if sex!=1

//Gen country of origin FE
forvalues j=1/129 {
	gen CFE_`j' =0
	replace CFE_`j'=1 if cnbr_b==`j' & nativity==2
}

cd "/IPUMS International/"
reg logw_earn CFE_* age age_sq age_qu MarBin i.edattaind#cnbr_h i.sample if sex==1 & empstat==1 & school!=1 &  edattaind!=999, vce(cluster cnbr_b)
parmest, format(estimate min95 max95 %8.2f p %8.1e) saving(w_earn, replace) omit empty 

reg log_inctot CFE_* age age_sq age_qu MarBin i.edattaind#cnbr_h i.sample if sex==1 & empstat==1 & school!=1 &  edattaind!=999, vce(cluster cnbr_b)
parmest, format(estimate min95 max95 %8.2f p %8.1e) saving(w_inctot, replace) omit empty 

save  "/IPUMS International/IPUMS_Regs.dta", replace //(to simplify re-run for frequencies)

//gen Freq. file
egen u = rowmiss(log_inctot age edattaind sample) 
keep if u==0 & nativity==2 & sex==1 & empstat==1 & school!=1

collapse (count) u, by(cnbr_b)
rename u Freq_inctot
save "/IPUMS International/IpInt_Freq.dta"


use "/IPUMS International/IPUMS_Regs.dta"
egen u = rowmiss(logw_earn age edattaind sample) 
keep if u==0 & nativity==2 & sex==1 & empstat==1 & school!=1

collapse (count) u, by(cnbr_b)
rename u Freq_w_earn

merge 1:1 cnbr using "/IPUMS International/IpInt_Freq.dta"
drop _merge

save "/IPUMS International/IpInt_Freq.dta", replace


/////////////
use "/IPUMS International/w_earn.dta", clear
gen delta =strpos(parm,"CFE_")
drop if omit==1
drop if delta==0
drop delta

gen par_nr= regexr(parm,"CFE_","")
destring par_nr, replace

rename par_nr cnbr
rename estimate w_earn
drop if stderr==0
keep w_earn  cnbr

merge 1:1 cnbr using "/IPUMS International/MacroData_ToMona.dta" 
drop _merge 
save "/IPUMS International/MacroData_IpInt.dta" 

use "/IPUMS International/w_inctot.dta", clear
gen delta =strpos(parm,"CFE_")
drop if omit==1
drop if delta==0
drop delta

gen par_nr= regexr(parm,"CFE_","")
destring par_nr, replace

rename par_nr cnbr
rename estimate w_inctot
drop if stderr==0
keep w_inctot  cnbr

merge 1:1 cnbr using "/IPUMS International/MacroData_IpInt.dta" 
drop _merge 
save "/IPUMS International/MacroData_IpInt.dta"  , replace

use "/IPUMS International/IpInt_Freq.dta"
rename cnbr_b cnbr
drop if cnbr==0
merge 1:1 cnbr using "/IPUMS International/MacroData_IpInt.dta" 
drop _merge
save "/IPUMS International/MacroData_IpInt.dta"  , replace



/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Results for Table 
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
eststo: reg w_earn TRP SSP  [aw=Freq_w_earn] if WVInd==1 & !missing(cognitive)  , vce(cluster cnbr) 
eststo: reg w_earn TRP SSP cognitive LifeExp FertRate  [aw=Freq_w_earn] if WVInd==1 & !missing(cognitive)  , vce(cluster cnbr) 

eststo: reg w_inctot TRP SSP  [aw=Freq_inctot] if WVInd==1 & !missing(cognitive)  , vce(cluster cnbr) 
eststo: reg w_inctot TRP SSP cognitive LifeExp FertRate  [aw=Freq_inctot] if WVInd==1 & !missing(cognitive) , vce(cluster cnbr) 
